package com.jxt.myjxtmanager.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.manager.Cache;
import com.jxt.myjxtmanager.manager.EventListener;
import com.jxt.myjxtmanager.ui.fragment.HomeWorkFragment;
import com.jxt.myjxtmanager.ui.fragment.NotificationFragment;

/**
 * Created by qm on 2017/3/4 0004.
 */
public class TodayknowActivity extends BaseActivity implements EventListener {

    private HomeWorkFragment workFragment;
    private Button homework, notice;
    private static final String TAG_homework = "homework";
    private static final String TAG_notice = "notice";
    private String currentFragTag = TAG_homework;// 当前fragment的tag
    /**
     * 用于对Fragment进行管理
     */
    public FragmentManager fragmentManager;
    private int msg_type = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_today_know;
    }

    @Override
    protected void initView() {
        Cache.put(this);
        setToolbarTitle("今日须知");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        homework = (Button) findViewById(R.id.btn_homework);
        notice = (Button) findViewById(R.id.btn_notice);
        homework.setOnClickListener(this);
        notice.setOnClickListener(this);

    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    protected void initData() {

        workFragment = new HomeWorkFragment();
        fragmentManager = getSupportFragmentManager();
        // 第一次启动时选中TAG_info
        fragmentManager.beginTransaction()
                .replace(R.id.off_map_frame, workFragment, TAG_homework).commit();
    }

    @Override
    protected void eventBundle(Bundle bundle) {
        super.eventBundle(bundle);
        if (bundle != null)
            msg_type = bundle.getInt("msg_type");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (msg_type == 1) {
            switchFrag(currentFragTag, TAG_homework);
        } else {
            switchFrag(currentFragTag, TAG_notice);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_homework:
                switchFrag(currentFragTag, TAG_homework);

                break;
            case R.id.btn_notice:
                switchFrag(currentFragTag, TAG_notice);

                break;
        }
    }

    enum FragTags {
        homework, notice;
    }

    private void switchFrag(String preFrag, String fragTag) {
        if (!preFrag.equals(fragTag)) {
            switchImg(fragTag);// 顶部导航图片高亮显示
            // 查找fragment栈，看是否该fragment已存在，存在show,不存在则新建
            Fragment tempFragment = fragmentManager.findFragmentByTag(fragTag);
            Fragment preFragment = fragmentManager.findFragmentByTag(preFrag);
            if (tempFragment == null) {
                FragTags tag = FragTags.valueOf(fragTag);
                String tagStr = "";
                Fragment storeFrag = null;
                switch (tag) {
                    case homework:
                        tagStr = TAG_homework;
                        storeFrag = new HomeWorkFragment();
                        break;
                    case notice:

                        tagStr = TAG_notice;
                        storeFrag = new NotificationFragment();
                        break;

                    default:
                        break;
                }
                fragmentManager.beginTransaction().hide(preFragment)
                        .add(R.id.off_map_frame, storeFrag, tagStr).commit();

            } else {
                fragmentManager.beginTransaction().hide(preFragment)
                        .show(tempFragment).commit();
            }
        }
        currentFragTag = fragTag;
    }

    // 底部TextView切换高亮
    private void switchImg(String fargTag) {
        FragTags currenttag = FragTags.valueOf(currentFragTag);
        switch (currenttag) {
            case homework:
                homework.setTextColor(getResources().getColor(R.color.txt_bule_color));
                homework.setBackgroundResource(R.drawable.btbg_circle_normal);
                break;
            case notice:
                notice.setTextColor(getResources().getColor(R.color.txt_bule_color));
                notice.setBackgroundResource(R.drawable.btbg_circle_normal);
                break;
        }

        FragTags tag = FragTags.valueOf(fargTag);
        switch (tag) {
            case homework:
                homework.setTextColor(getResources().getColor(R.color.white));
                homework.setBackgroundResource(R.drawable.btbg_circle_press);
                break;
            case notice:
                notice.setTextColor(getResources().getColor(R.color.white));
                notice.setBackgroundResource(R.drawable.btbg_circle_press);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Cache.remove(this);
    }

    @Override
    public void onMessageNotification(String event, int status, int unReadMessage) {

    }

    @Override
    public String getMessageNotification() {
        return null;
    }
}
